'''
Created on Apr 8, 2011

@author: yc30v1m
'''
from logging import getLogger
from library.Utils import singleton
from plugins.testlink.TestLinkScriptParser import TestLinkScriptParser

log = getLogger(__name__)


@singleton
class TestLink(object):
    '''
    classdocs
    '''
    DEFAULT_PROJECT_NAME_LIKE = 'PSP'
    DEFAULT_TESTPLAN_NAME_LIKE = 'First'
    DEFAULT_BUILD_NAME_LIKE = 'Today'
    DEFAULT_ATTACHED_SCRIPT_FILE_NAME = 'Python'
    API_KEY = "56ba98ccb8727d29f7e4dbcb8468ff5c"
    
    _devKey = ''
    
    

    def __init__(self,key=None, tlUrl=None):
        
        if key == None:
            self._setApiKey()
        else:
            self._setApiKey(key)
        
        self.tl = TestLinkScriptParser(self._devKey,tlUrl )
        #self.setupDefaults()
       
    def getAllTestCaseIds(self):
        return self.tl.getTestCaseIdsForTestPlan()
    
    def setupDefaults(self):
        self.setProjectName()
        self.setTestPlanName()
        self.setBuildName()
        self.setAutomationScriptFileName()
    
    def setTCID(self,tcid):
        return self.tl.setTCID(tcid)
    
    def getSectionAndOption(self,section,option):
        return self.tl.getSectionAndOption(section, option)
    
    def setTCResult(self,result):
        self.tl.setTCResult(result)
        
    def setProjectName(self, projectName = DEFAULT_PROJECT_NAME_LIKE):
        if projectName:
            self.tl.setProjectName(projectName)      
        
    def setTestPlanName(self,testPlanName = DEFAULT_TESTPLAN_NAME_LIKE):
        if testPlanName:
            self.tl.setTestPlanName(testPlanName)
        
    def setBuildName(self,buildName = DEFAULT_BUILD_NAME_LIKE):
        if buildName:
            self.tl.setBuildName(buildName)
        
    def setAutomationScriptFileName(self,attachedScriptFileName=DEFAULT_ATTACHED_SCRIPT_FILE_NAME):
        
        if attachedScriptFileName:
            self.tl.setAutomationScriptFileName(attachedScriptFileName)
    
    def _setApiKey(self, key = API_KEY):
        self._devKey = key
        
'''      
if __name__ == '__main__':
    try:
        sample = TestLink()
        sample.setProjectName('API')
        sample.setTestPlanName('PSP')
        print sample.getAllTestCaseIds()
        print sample.setTCID('APIMT 2-1')
        print sample.getSectionAndOption('SECTION1', 'query')
        print sample.setTCResult('p')
    except Exception, v:
        print "WOW : ", str(v)
'''